#include "debug_show_booklist.h"
#include "ui_debug_show_booklist.h"
#include "bookstore.h"
#include "utils.h"
#include <vector>

using namespace utils;

debug_show_bookList::debug_show_bookList(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::debug_show_bookList) {
    ui->setupUi(this);
}

debug_show_bookList::~debug_show_bookList() {
    delete ui;
}

void debug_show_bookList::on_pushButton_clicked() {
    loadBookList();
}

void debug_show_bookList::loadBookList() {
    auto size = sizeof (Book);//学生类的大小
    FILE *file = openfile(bookDatPath, size); //打开文件

    fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
    auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）

    int row = offset / size; //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(7); //设置列数
    QStringList header;
    header << "ID" << "对应书目ID" << "exist" << "isBorrowed" << "isBooked" << "borrowNum"<<"bookcond";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数



    for(int i = 0; i < row; i++) {
        Book* tempBook = bookstore::createBook();

        fseek(file, size * i, SEEK_SET);   //将指针移到该放的地方
        fread(tempBook, size, 1, file);          //读出需要查询的同学数据

        QString ID = QString::number(tempBook->ID);
        QString menu_id = QString::number(tempBook->menu_ID);
        QString exist = QString::number(tempBook->exist);
        QString isBorrowed = QString::number(tempBook->isBorrowed);
        QString isBooked = QString::number(tempBook->isBooked);
        QString borrow_num = QString::number(tempBook->borrow_num);
        QString bookcond=QString::number(tempBook->book_cond);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(ID));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(menu_id));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(exist));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(isBorrowed));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(isBooked));
        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(borrow_num));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(bookcond));

        delete(tempBook);
    }
    fclose(file);//关闭文件
}
